package DSENS;

/**
 *
 * @author teeteto
 */

import java.util.ArrayList;


public class HierarchicalNode extends Node{

    private double CompPower;
    private double EnergyPower;
    private double pheromone;
    private double pherVector[];
    private ArrayList<Event> eventQueue;
    private int eventIndex;
    Log l;

    public HierarchicalNode(int CompPower, double EnergyPower, double pheromone, double[] pherVector, ArrayList<Event> eQ) {
        super();
        this.CompPower = 100;
        this.EnergyPower = 1000;
        this.pheromone = 0;
        this.pherVector = pherVector;
        this.eventQueue = eQ;
        this.eventIndex=0;
        this.l= new Log();
    }

    
 
    public double getCompPower() {
        return CompPower;
    }

    public void setCompPower(double CompPower) {
        this.CompPower = CompPower;
    }

    public double getEnergyPower() {
        return EnergyPower;
    }

    public void setEnergyPower(double EnergyPower) {
        this.EnergyPower = EnergyPower;
    }

    public double[] getPherVector() {
        return pherVector;
    }

    public void setPherVector(double[] pherVector) {
        this.pherVector = pherVector;
    }

    public double getPheromone() {
        return pheromone;
    }

    public void setPheromone(double pheromone) {
        this.pheromone = pheromone;
    }
    
     
    public NodeState powerANDpherManagement(int i) throws LogException {
        this.EnergyPower= this.EnergyPower -i;
        this.pheromone = this.pheromone +i;
        if(this.EnergyPower <= 0){
            this.setState(NodeState.OFF);
            this.l.printlnLog("Battery OUT...shutdown node");
            return NodeState.OFF;
        }else return this.getState();
    }

    @Override
    public void manageEvent(Event e) {
        int ttl;
        NodeState ns;
        try{
            if(this.getState().equals(NodeState.ON)){
                ns = this.powerANDpherManagement(100);
                if(this.getState().equals(NodeState.OFF)) return;
                else{
                    this.setState(NodeState.WORKING);
                    l.printEvent(e, "Begin execution of event "+e.getID());
                    e.seteventType("internal");
                    GlobalInfo.TIMELINE.addEvent(e.getMessage().getTimetolive(), e);
                }
             }else if(this.getState().equals(NodeState.WORKING)){
                 if(e.geteventType().equals("internal")){
                    l.printlnLog("Endind execution of event"+e.getID());
                    l.printlnLog("checking for other events");
                    if(this.eventIndex<this.eventQueue.size()){
                        e = this.eventQueue.get(this.eventIndex);
                        this.eventIndex++;
                        l.printEvent(e, "getting event from node queue");
                        ns = this.powerANDpherManagement(100);
                        if(this.getState().equals(NodeState.OFF)) return;
                        else{
                            l.printEvent(e, "Begin execution of event "+e.getID());
                            e.seteventType("internal");
                            GlobalInfo.TIMELINE.addEvent(e.getMessage().getTimetolive(), e);
                        }
                    }
                    else{
                        l.printlnLog("No events found");
                        this.setState(NodeState.ON);
                    }
                 }
             }
          }catch (Exception ex) {ex.printStackTrace();}
    }
    

    @Override
    public void printNode() {
        super.printNode();
    }

    @Override
    public void receiveEvent(Event e) {
        NodeState ns;
        try {
            if(this.getState().equals(NodeState.ON)){
                l.printEvent(e, "receiving event");
                ns = this.powerANDpherManagement(100);
                if(this.getState().equals(NodeState.OFF)) return;
            }
            else if(this.getState().equals(NodeState.WORKING)){
                if(e.geteventType().equals("internal")) return;
                else{
                    l.printEvent(e, "receiving event");
                    ns = this.powerANDpherManagement(100);
                    if(this.getState().equals(NodeState.OFF)) return;
                    else{
                        this.eventQueue.add(e);
                        l.printEvent(e, "event added in node queue");
                    }
                }
            }
         }catch (Exception ex) {ex.printStackTrace();}
    }
    
}